package de.karlNet.login.attributeMapper;

import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.stereotype.Component;

import de.karlNet.Enums.BinaryMaskEnumMapper;
import de.karlNet.login.Useraccountcontrol;
import de.karlNet.sambamanager.ldapModel.LdapObject;
import de.karlNet.sambamanager.ldapModel.LdapUser;

@Component
public class UserExtractor extends LdapObjectExtractor {
	private BinaryMaskEnumMapper<Useraccountcontrol> mapper = new BinaryMaskEnumMapper<Useraccountcontrol>(
			Useraccountcontrol.class);
	@Override
	public LdapObject extract(Attributes attributes, List<String> objectClasses)
			throws NamingException {
		if (objectClasses.contains("user")) {
			String dn = this.tryToGetString("distinguishedname", attributes,
					tryToGetString("dn", attributes));
			String cn = this.tryToGetString("name", attributes,
					tryToGetString("cn", attributes));
			String firstname = this.tryToGetString("givenName", attributes);
			String lastname = this.tryToGetString("sn", attributes);
			String username = this.tryToGetString("samaccountname", attributes);
			int userAccountValue = Integer.parseInt(this.tryToGetString(
					"userAccountControl", attributes));
			return new LdapUser(cn, dn, username, firstname, lastname,
					this.mapper.getEnums(userAccountValue));
		}
		return null;
	}
}
